import Vue from 'vue'
import Vuex from 'vuex'
import {Message} from 'element-ui';
import ygrStore from './backModules/ygr/ygrStore.js'
import lypStore from './backModules/lyp/lypStore.js'
import hjwStore from './backModules/hjw/hjwStore.js'
import cfStore from './frontModules/cf/cfStore.js'
import lajStore from './frontModules/laj/lajStore.js'
import table from './common/table.js'
import http from  '../http/http'
import * as url from '../api/cfApi'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
  	outmsg:'outmsg',
    loginUser: sessionStorage.getItem('loginUser')||'111',
    jobName: sessionStorage.getItem('jobName')||undefined,
    userName: sessionStorage.getItem('userName')||undefined,
    isBack:true,
    redirects:{
      "主管":'/foods',
      "经理":'/foods',
      "会计":'/finance',
      "服务员":'/frontHome',
      "领班":'/frontHome',
    }
  },
  mutations: {
  	outtest(){
  		console.log('out mutations')
  	},
    loginMu(state,data) {
  	  //将数据写到vuex当中
      state.loginUser = data.staff.id
      state.jobName = data.staff.job.name //TODO: 后面需要换成 jobName
      state.userName = data.staff.name
      state.roleId = data.staff.roleId
    }
  },
  actions: {
  	outtest({commit}){//测试在命名空间的局部内拿到全局actions方法
  		commit('outtest')
  		console.log('out actions')
  	},
    loginAc ({commit}, obj) {
      return http({
        url: '/api' + url.LOGIN,
        method: 'post',
        data: obj
      }).then(res => {
        if (res.data.code === '200' ) {
        //  登录成功,将登录信息的数据写到 本地
          sessionStorage.setItem('loginUserId', res.data.staff.id)
          sessionStorage.setItem('jobName', res.data.staff.job.name)// TODO:后面需要换成 jobName
          sessionStorage.setItem('userName', res.data.staff.name)
          sessionStorage.setItem('roleId', res.data.staff.roleId)
          sessionStorage.setItem('loginAllInfo',JSON.stringify(res.data.staff))
          commit('loginMu', res.data)
        }
        return res.data
      }).catch(err => {
        console.log(err)
      })
    },
    verifyNumberAC ({commit},obj) {
      return http({
        method: 'post',
        url: '/api'+ url.LOGIN_NUMBER_CONFIRM,
        data: obj
      }).then(res => {
        console.log('登录账号验证',res)
        if (res.data.code !== '200') {
          return {message: '该账号不存在'}
        }
        return {message: '可以登录'}
      }).catch(err => {
        console.log(err)
      })
    }
  },
  modules: {
  	ygrStore,
  	lypStore,
  	hjwStore,
  	cfStore,
  	lajStore,
    table
  }
})
